package auctionhaus

import grails.test.*
import com.auctionhaus.*
import static junit.framework.Assert.assertFalse
import static groovy.util.GroovyTestCase.assertEquals
import static junit.framework.Assert.assertTrue

class CustomerTests extends  GroovyTestCase {

    transient Date testDate =  new Date((2011-1900), 01, 15); //02-15-2011
    transient Date genericDate = new Date()
    protected void setUp() {
        super.setUp()

        deleteAllFromDatabase()
    }

    protected void tearDown() {
        super.tearDown()
    }

    private void deleteAllFromDatabase() {

        Bid.findAll().each {x -> x.delete() }
        Listing.findAll().each {x -> x.delete() }
        Customer.findAll().each {x -> x.delete() }
    }

    void testSomething() {

    }

    // Requirement C-3
    void testAddDuplicateEmailAddressFails()
    {
        // create Customer with a non unique field
		def customerA = new Customer(emailAddress: "customerAEmailAddress@yahoo.com",
									password: "abcdefg",
									date:genericDate)


        customerA.save()

        // Assert that we found CustomerA
        assertEquals Customer.findAll().size(), 1

        // create Customer with a non unique field
		def customerB = new Customer(emailAddress: "customerAEmailAddress@yahoo.com",
									password: "abcdefg",
									date:genericDate)

        // Try to save customerB with the same email address as customerA
        customerB.save()

        assertEquals 1, customerB.errors.errorCount
        assertEquals 'emailAddress', customerB.errors.fieldError.field
    }

    // Requirement C-3
    void testAddInvalidEmailAddress()
    {
        // Missing "@"

        // create Customer with a non unique field
		def customerA = new Customer(emailAddress: "customerAEmailAddressyahoo.com",
									password: "abcdefg",
									date:genericDate)


        customerA.validate()

        assertEquals 1, customerA.errors.errorCount
        assertEquals 'emailAddress', customerA.errors.fieldError.field


        // Missing "."

        // create Customer with a non unique field
		def customerB = new Customer(emailAddress: "customerBEmailAddress@yahoocom",
									password: "abcdefg",
									date:genericDate)

        // Try to save customerB with the same email address as customerA
        customerB.validate()

        assertEquals 1, customerB.errors.errorCount
        assertEquals 'emailAddress', customerB.errors.fieldError.field
    }

	// Customer Domain Reqs
	// C-2: Email address must be a unique field (integration test) 
	void testEmailAddressUniqueFields()
	{
		// create Customer with a non unique field
		def customerA = new Customer(emailAddress: "email@gmail.com",
									password: "abcdefg", 
									date:testDate).save()

		def customerB = new Customer(emailAddress: "email@gmail.com",
									password: "abcdefg",
									date:testDate)


		// make sure the validation fails
		assertFalse customerB.validate()
		assertTrue customerB.errors.hasFieldErrors("emailAddress")
	}
	
	// Customer Domain Reqs 
	// C-3: Email address must be of a valid form (*@*.*) (integration test) 
	void testEmailAddressIsValidForm()
	{
		// create Customer with an invalid email form
		def customer = new Customer(emailAddress: "email@gmail.com", password: "abcdefg", date:testDate).save()
		
		// make sure the validation fails
		assertTrue 'Validation should succeed', customer.validate()
        assertFalse customer.hasErrors()
		assertFalse customer.errors.hasFieldErrors("emailAddress")
	}
	
	// Customer Domain Reqs C-4
	//C-4: Password must be between 6-8 characters (integration test) 
	void testPasswordCharacterAndLength()
	{
		// create Customer with an invalid password and character length
		def customerA = new Customer(emailAddress: "email@gmail.com", password: "12d34", date:testDate)
		
		// make sure the validation fails
		assertFalse 'Validation should have failed', customerA.validate()

        customerA.validate()
        assertEquals Customer.findAll().size(), 0
        assertTrue customerA.hasErrors()
        assertTrue customerA.errors.hasFieldErrors("password")

        // create Customer with an invalid password and character length
		def customerB = new Customer(emailAddress: "email@gmail.com", password: "abcdefghi", date:testDate)

		// make sure the validation fails
		assertFalse 'Validation should have failed', customerB.validate()

        customerB.validate()
        assertEquals Customer.findAll().size(), 0
        assertTrue customerB.hasErrors()
        assertTrue customerB.errors.hasFieldErrors("password")
	}

    void testPasswordIsValid()
    {
        // create Customer with a non unique field
		def customerA = new Customer(emailAddress: "customerAEmailAddress@yahoo.com",
									password: "abcdefg",
									date:testDate).save()

        // Assert that we found CustomerA
        assertEquals Customer.findAll().size(), 1
        assertFalse customerA.hasErrors()
    }
}
